home *** CD-ROM | disk | FTP | other *** search
- Path: news.rain.org!usenet
- From: "Guus Leeuw jr." <guusl@eiffel.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Deep/Shallow Copying?
- Date: Thu, 11 Jan 1996 08:25:05 -0800
- Organization: ISE Inc. http://www.eiffel.com
- Message-ID: <30F539E1.661A57DB@eiffel.com>
- References: <4d1bhe$1lto@bocanews.bocaraton.ibm.com>
- NNTP-Posting-Host: @outback.eiffel.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b3 (X11; I; Linux 1.2.8 i586)
-
- pani@genius.tisl.soft.net wrote:
- >
- > Hi Guys,
- > What is Deep copying or Shallow copying? Is this something
- > to do with a Copy constructor?
- > Thanks for any help.
- > Pani.
-
- A deep copy is a copy where not only the current object (`this') is
- copied but also all, I repeat all, objects that `this' references.
-
- A shallow copy is a copy where the current object (`this') is copied
- but the pointers to other objects are kept as they are (i.e. referenced
- objects will not be copied).
-
- You can simulate both kinds of behavior in your copy constructor.
-
- For a deep copy, you will need copy constructors for the classes of
- the referenced objects aswell. That will result in a chain of calls to
- copy constructors.
-
- For example:
- class A
- {
- public:
- A(); // std ctor
- A(A&); // copy ctor
-
- B *b;
- C *c;
- }
-
- A::A(A& other)
- {
- b = new B(*(other.b));
- c = new C(*(other.c));
- }
-
- Remark that the copy process will go down the whole branch of objects
- (i.e. if B has a pointer to an object of class D, that object has to be
- copied as well).
-
- For a shallow copy, however, you just assign the pointer values form
- `this' to the new object of the same type as `this'.
-
- For example:
- same class A;
-
- A::A(A&)
- {
- b = other.b;
- c = other.c;
- }
-
-
- Hope this explains,
- Guus Leeuw jr.
-